import router from '@/router'

// 跳转方法
export const nextPage = (url, param) => {
    let obj = {path: url, query: param}
    router.push(obj)
}

// 时间格式化
export const getDateTime = (ms, val) => {
    let Y = ms.getFullYear()
    let M = ms.getMonth() + 1
    if (M < 10) {
        M = '0' + M
    }
    let D = ms.getDate()
    if (D < 10) {
        D = '0' + D
    }
    let h = ms.getHours()
    if (h < 10) {
        h = '0' + h
    }
    let m = ms.getMinutes()
    if (m < 10) {
        m = '0' + m
    }
    let s = ms.getSeconds()
    if (s < 10) {
        s = '0' + s
    }
    if (val === 's') {
        return Y + M + D + h + m + s
    } else if (val === 'ss') {
        return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
    } else {
        return Y + '-' + M + '-' + D + ' ' + h + ':' + m
    }
}

// 时间格式化，时间自增
export const getFormatTime = (ms, vm) => {
    let Y = ms.getFullYear()
    let M = ms.getMonth() + 1
    let D = ms.getDate()
    let h = ms.getHours()
    let m = ms.getMinutes()
    let s = ms.getSeconds()
    if (M <= 9) {
        M = `0${M}`
    }
    if (D <= 9) {
        D = `0${D}`
    }
    setInterval(() => {
        s++
        if (s < 10) {
            s = `0${s}`
        } else if (s > 59) {
            s = 0 + '0'
            m++
        }
        if (m <= 9 && (m + '').length < 2) {
            m = `0${m}`
        } else if (m > 59) {
            m = 0
            h++
        }
        if (h <= 9 && (h + '').length < 2) {
            h = `0${h}`
        } else if (h > 24) {
            h = 1
        }
        vm.time = Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
    }, 1000)
    return vm.time
}

// 数字千位符格式化
export const toThousands = (num) => {
    let result = []
    let counter = 0
    num = (num || 0).toString().split('')
    for (let i = num.length - 1; i >= 0; i--) {
        counter++
        result.unshift(num[i])
        if (!(counter % 3) && i !== 0) {
            result.unshift(',')
        }
    }
    return result.join('')
}

// 日期格式化
export const formatTransDate = (date) => {
    const data = date.replace(/^(\d{4})(\d{2})(\d{2})$/, '$1.$2.$3')
    return data
}

// 后台树到前台vue树的数据结构转换
export const transTreeDataToVue = (treeNodes) => {
    let layerTreeDatas = []
    for (let treeNode of treeNodes) {
        if (!treeNode.obj) {
            throw Error('数据格式不正确!')
        }
        let layerTreeData = JSON.parse(JSON.stringify(treeNode.obj))
        delete layerTreeData.children
        layerTreeDatas.push(layerTreeData)

        let children = treeNode.children
        if (children && children.length) {
            let childLayerTreeDatas = transTreeDataToVue(children)
            layerTreeData['children'] = childLayerTreeDatas
        }
    }
    return layerTreeDatas
}

/**
 * 随机生成16进制的颜色代码
 * @returns {string}
 */
export function randomColor () {
    let colorArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
    let color = '#'
    for (let i = 0; i < 6; i++) {
        color = color + colorArray[Math.floor(Math.random() * 16)]
    }
    return color
}

/**
 * GUID生成
 */
export function guid () {
    let s4 = () => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
    return (s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4())
}

/**
 功能：模板字符串填充
 示例:
 tplStrFill('CC:#{row[1].data.name}CC:#{dd.name}CC:#{456}CC:#{uu[0][0].name}__', {
        row: [{data: {name: '李四'}}, {data: {name: '王五'}}],
        dd: {name: '啦啦'},
        456: '丫丫丫丫',
        uu: [[{name: 123}]]
    })
 输出: "CC:王五CC:啦啦CC:丫丫丫丫CC:123__"
 * @param templateStr
 * @param dataBean
 * @returns {*}
 */
export function tplStrFill (templateStr, dataBean) {
    try {
        if (templateStr) {
            let indexRegExp = new RegExp('\\[\\d*\\]', 'g')
            let exprList = templateStr.match(new RegExp('#{(\\w|\\.|\\[?|\\]?)*}', 'g')) || []
            for (let expr of exprList) {
                let exprContent = expr.substring(2, expr.length - 1)
                let schemaList = exprContent.split('.')
                let dataBeanTemp = dataBean
                for (let schema of schemaList) {
                    if (schema) {
                        let indexList = schema.match(indexRegExp)
                        if (indexList && indexList.length) {
                            schema = schema.substring(0, schema.search(indexRegExp))
                            dataBeanTemp = dataBeanTemp[schema]
                            for (let indexT of indexList) {
                                let index = indexT.substring(1, indexT.length - 1)
                                dataBeanTemp = dataBeanTemp[index]
                            }
                        } else {
                            dataBeanTemp = dataBeanTemp[schema]
                        }
                    } else {
                        throw new Error('表达式语法错误！~')
                    }
                }
                let exprValue = dataBeanTemp || ''
                templateStr = templateStr.replace(`#{${exprContent}}`, exprValue)
            }
        }
    } catch (e) {
        throw new Error('模板存在语法错误！~')
    }

    return templateStr
}
